package com.jy.appmarket.cards;

import java.util.List;
import java.util.Random;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;

import com.imbryk.viewpager.LoopViewPager;
import com.jy.app.market.apk.type.LinkType;
import com.jy.app.market.idata.card.CardGroupBanner;
import com.jy.app.market.idata.data.Banner;
import com.jy.appmarket.R;
import com.jy.appmarket.activity.AppDetailActivity;
import com.jy.appmarket.activity.CardPageActivity;
import com.jy.appmarket.activity.PhoneEssentialActivity;
import com.jy.appmarket.activity.WebViewActivity;
import com.jy.appmarket.statistics.ApkSource;
import com.jy.appmarket.utils.LoadImageUtils;
import com.jy.appmarket.utils.UITools;
import com.jy.appmarket.widget.CirclePageIndicator;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * 类说明：
 * 
 * @author Baker.li
 * @date 2015年8月21日
 * @version 1.0
 */

public class GroupBannerUICard extends BaseUICard {
	private CardGroupBanner mGroupBanner;
	private LoopViewPager mPager;
	private BannerAdapter mAdapter;
	private CirclePageIndicator mIndicator;

	public GroupBannerUICard(Context context, CardGroupBanner gb) {
		super(context, R.layout.group_banner_card_layout);
		mGroupBanner = gb;
		if (getContext() instanceof FragmentActivity && mGroupBanner.getBanners() != null) {
			mAdapter = new BannerAdapter(getContext(), mGroupBanner.getBanners());
			mGroupBanner.getBanners();
		}
		setShadow(false);
	}

	@Override
	public int getType() {
		return 9;
	}

	@Override
	public void setupInnerViewElements(ViewGroup parent, View view) {
		if (mAdapter == null) {
			return;
		}
		mPager = parent.findViewById(R.id.group_banner_viewpager);
		
		int pageW = UITools.getDeviceScreenWidthHeight(getContext())[0];
		int pageH = getHeaderImageHeight();
		ViewGroup.LayoutParams lp = mPager.getLayoutParams();
		lp.width = pageW;
		lp.height = pageH;
		mPager.setLayoutParams(lp);
		
		mIndicator = parent.findViewById(R.id.group_banner_indicator);

		mPager.setAdapter(mAdapter);
		if (mGroupBanner.getBanners().size() < 2) {
			mIndicator.setVisibility(View.GONE);
		} else {
			mIndicator.setVisibility(View.VISIBLE);
			mIndicator.setViewPager(mPager);
		}
		int index = mGroupBanner.getIndex();
		int bannerSize = mGroupBanner.getBanners().size();
		if (index < 0 || index >= bannerSize) {
			index = new Random().nextInt(bannerSize);
		}
		mPager.setCurrentItem(index);
	}
	
	private int getHeaderImageHeight(){
		int [] screenSize = UITools.getDeviceScreenWidthHeight(getContext());
		int screenW = screenSize[0];
		
		float h = ((float)screenW * 3) / 7; // 宽高比为7:3
		
		return (int)h;	
	}

	public class BannerAdapter extends PagerAdapter {
		private Context mContext;
		private List<Banner> mList;

		public BannerAdapter(Context context, List<Banner> banners) {
			mContext = context;
			mList = banners;
		}

		@Override
		public int getCount() {
			return mList.size();
		}

		@Override
		public int getItemPosition(Object object) {
			return POSITION_NONE;
		}

		@Override
		public View instantiateItem(ViewGroup container, final int position) {

			final Banner b = mList.get(position % mList.size());
			LayoutInflater li = LayoutInflater.from(mContext);
			View view = li.inflate(R.layout.image_banner_layout, container, false);
			ImageView icon = view.findViewById(R.id.image_banner_image);
			
			ImageLoader.getInstance().displayImage(b.getImageUrl(), icon,
					LoadImageUtils.getBannerDisplayOption(getContext()));

			icon.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					if (LinkType.App == b.getType()) {
						Intent detail = new Intent(getContext(), AppDetailActivity.class);
						detail.putExtra(AppDetailActivity.EXTRA_NAME, b.getTitle());
						detail.putExtra(AppDetailActivity.EXTRA_PKG, b.getUrl());
						detail.putExtra(AppDetailActivity.EXTRA_SOURCE,
								ApkSource.toSourceString(getApkSource()));
						getContext().startActivity(detail);

					} else if (LinkType.Cards == b.getType()) {
						if ("essential".equals(b.getUrl())) {
							getContext().startActivity(
									new Intent(getContext(), PhoneEssentialActivity.class));
						} else {
							Intent card = new Intent(getContext(), CardPageActivity.class);
							card.putExtra(CardPageActivity.EXTRA_TITLE, b.getTitle());
							card.putExtra(CardPageActivity.EXTRA_PATH, b.getUrl());
							getContext().startActivity(card);
						}
					} else if (LinkType.WebView == b.getType()) {
						Intent detail = new Intent(getContext(), WebViewActivity.class);
						detail.putExtra(WebViewActivity.EXTRA_TITLE, b.getTitle());
						detail.putExtra(WebViewActivity.EXTRA_URL, b.getUrl());
						getContext().startActivity(detail);

					}
				}
			});
			container.addView(view, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			return view;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}
	}
}